package com.kt.thread.demo;

/**
 * Created by tangwy on 2016/3/26.
 */
public class DeadLockTest {
    public static void test() {
        final A a = new A();
        final B b = new B();
        new Thread(new Runnable() {
            @Override
            public void run() {
                a.a(b);
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                b.b(a);
            }
        }).start();

    }

    static class A {
        public synchronized void a(B b) {
            System.out.println("进入了A实例的a方法");
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            b.b2();
        }

        public synchronized void a2() {
            System.out.println("进入了A实例的a2方法");
        }
    }

    static class B {
        public synchronized void b(A a) {
            System.out.println("进入了B实例的b方法");
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            a.a2();
        }

        public synchronized void b2() {
            System.out.println("进入了B实例的b2方法");
        }
    }
}


